<?php 
	class Users extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

		function insert($data){
       		$result = $this->db->insert('user', $data);
       		return $result;
		}

		function get_user_vlist($limit = 0, $offset = 10){
			$sql = "SELECT * FROM {$this->db->dbprefix}user_idcard ORDER BY status ASC, id DESC LIMIT $limit, $offset";
			$result = $this->db->query($sql)->result_array();
			$sql = "SELECT count(id) AS count FROM {$this->db->dbprefix}user_idcard";
			$count = $this->db->query($sql)->row_array();
       		return array("count" => $count['count'], "result" => $result);
		}

		function get_user_list($limit = 0, $offset = 10, $data = 0){
			$where = "WHERE 1 = 1";
			$order = 'id DESC';
			if(!empty($data['search'])){
				$where .= " AND (nick LIKE '%".$data['search']."%' OR mobile='".$data['search']."')";
			}

			if(isset($data['channle_search'])&&!empty($data['channle_search'])){
				$channle_info=get_field_by_where('channel_statis','channel_key',"name='".$data['channle_search']."'");
				if(empty($channle_info)){
					$data['need_channle_search']=1;
				}else{
					$where .= " AND channel_name='".$channle_info['channel_key']."' ";	
				}
			}
			
			if(isset($data['need_channle_search'])&&$data['need_channle_search']){
				$where .= " AND channel_name<>'' ";
			}
			
			if(isset($data['sort'])){
				$sort_order = array(1 => 'DESC', 2 => 'ASC');
				switch ($data['sort']) {
					case 1:
						$order = 'sex ' . $sort_order[$data['asc_des']];
						break;
						break;
				}
			}

			if(isset($data['anchor'])){
				$where .= " AND anchor = 1";
			}
			
			if(isset($data['team_id'])){
				$where .= " AND team_id = ".$data['team_id'];
			}

			if(isset($data['is_live'])&&$data['is_live']==1){
				$where .= " AND is_push = 1 AND is_live=1 ";
			}
			
			if(isset($data['is_verified'])){
				if($data['is_verified']==1){
					$where .= " AND verified_anchor = 1 ";	
				}elseif($data['is_verified']==9){
					$where .= " AND verified_anchor != 1 ";
				}
			}

			if(!empty($data['school_id'])){
				$where .= " AND school_id = '{$data['school_id']}' ";
			}
			
			if(isset($data['begin_time'])&&$data['begin_time']>0){
				$where.='  AND u.add_time>='.$data['begin_time'].' ';
			}
			if(isset($data['end_time'])&&$data['end_time']>0){
				$where.='  AND u.add_time<='.$data['end_time'].' ';
			}

			$this->db->trans_start();
			
			$sql = "SELECT SQL_CALC_FOUND_ROWS u.*,s.school_name,(select mobile from {$this->db->dbprefix}user where id= u.parent1) as parent1_mobile,
            (select count(0) from {$this->db->dbprefix}user where parent1= u.id) as parent1_count,
            (select count(0) from {$this->db->dbprefix}user where parent2= u.id) as parent2_count,
            (select count(0) from {$this->db->dbprefix}user where parent3= u.id) as parent3_count FROM {$this->db->dbprefix}user AS u left join {$this->db->dbprefix}o2o_school s on s.id=u.school_id ".$where."  order by $order LIMIT $limit, $offset";
			$result = $this->db->query($sql)->result_array();
			if(isset($data['search'])){
				foreach ($result as $key => $value) {
					$result[$key]['nick'] = str_replace($data['search'],'<font color="red">'.$data['search'].'</font>',$value['nick']);
				}
			}
			// $sql = "SELECT count(id) AS count FROM {$this->db->dbprefix}user as u ".$where."";

			 $count =  $this->db->query('SELECT FOUND_ROWS() as count;')->row_array();

			 $this->db->trans_complete();
			// $count = $this->db->query($sql)->row_array();
       		return array("count" => $count['count'], "result" => $result);
		}

		function get_user_info($id){
			$sql = "SELECT u.* FROM {$this->db->dbprefix}user AS u WHERE u.id=$id";
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		function delete($id){
			$this->db->where('id', $id);
			$result = $this->db->delete('user');
			return $result;
		}

		function batch_delete($ids){
			$ids = implode(",",$ids);
			$sql = "DELETE FROM {$this->db->dbprefix}user WHERE id in($ids)";
			$this->db->query($sql);
		}


		/**
		* 函数功能介绍：更新会员启用禁用
		* 创建人: 	Yang Ningshu
		* 创建时间: 2014-09-04 10:08
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		* @return array
		*/
		function update_user_status($id,$data){
			$this->db->where('id', $id);
 			return $this->db->update('user', $data);
		}
		
		function get_user_nick($where = '') {
			$sql = "SELECT * FROM ".$this->db->dbprefix('user')." WHERE $where";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}
		function update_user_nick($id = 0,$nick = '') {
			$sql = "UPDATE {$this->db->dbprefix}user SET nick='". $nick ."' WHERE id=$id";
			$this->db->query($sql);
		}

		/**
		* 函数功能介绍：增加用户视频总数
		* 创建人: Zhou Hailong
		* 创建时间: 2014-11-19 14:49
		* 最后修改人：
		* 最后修改时间：
		*/
		function plus_user_video_number($id){
			$sql = "UPDATE {$this->db->dbprefix}user SET v_count=v_count+1 WHERE id=$id";
			$this->db->query($sql);
		}

		function reduce_user_video_number($id){
			$sql = "UPDATE {$this->db->dbprefix}user SET v_count=v_count-1 WHERE id=$id";
			$this->db->query($sql);
		}

		/**
		* 函数功能介绍：获取该用户在推荐关系表的数据
		* 创建人: Yang Ningshu
		* 创建时间: 2015-04-24 14:49
		* 最后修改人：
		* 最后修改时间：
		*/
		function get_recommend_info($id, $recommended_place, $type){
			$sql = "SELECT id FROM {$this->db->dbprefix}recommended where top_id = ".$id." and recommended_place = ".$recommended_place."  and type = ".$type."";
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		/**
		* 函数功能介绍：增加用户相片数
		* 创建人: Zhang Shaowei
		* 创建时间: 2016-03-30 10:11
		* 最后修改人：
		* 最后修改时间：
		*/
		function increase_a_count($uid){
			$sql = "UPDATE {$this->db->dbprefix}user SET a_count = a_count + 1 WHERE id = $uid";
			$this->db->query($sql);
		}

		function reduce_a_count($uid, $a_count = 1){
			$sql = "UPDATE {$this->db->dbprefix}user SET a_count = a_count - $a_count WHERE id = $uid";
			$this->db->query($sql);
		}
    }